package com.fairytask.ui.item.pet
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.ResConfig;
	
	import flash.display.Sprite;
	
	/**
	 * 九字真言格
	 * @author xujh
	 * 
	 */
	public class NineItem extends Sprite
	{
		private var _item		:Array = [];
		//字大小
		private const WORD_SIZE	:int = 47;
		private var _bg		:Clip;
		//高亮底板
		private var _highBg	:Clip;
		//动画速度
		private const PLAYSPEED	:Number = 0.5;
//		private var timeLine	:TimelineLite;
		private var _posArr	:Array;
		private var _func	:Function;
		private var _showMoney	:Boolean = true;
		
		public function NineItem()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_bg = new Clip();
			_bg.source = ResConfig.uiBGPath + "wordsBG_9.png";
			addChild(_bg);
			//高亮蒙版
			_highBg = new Clip();
			_highBg.source = ResConfig.wordsPath + "BG.png";
			addChild(_highBg);
			_highBg.visible = false;
			initWord();
		}
		
		/**
		 * 初始化9个字 
		 * 
		 */
		private function initWord():void
		{
			for(var i:int=0;i<9;i++)
			{
				var word	:WordItem = new WordItem(0,WORD_SIZE,WORD_SIZE);
				word.x = 2+WORD_SIZE*(i%3)*1.04;
				word.y = 2+WORD_SIZE*Math.floor(i/3)*1.04;
				word.able = false;
				addChild(word);
				_item[i] = word;
			}
		}
		
		/**
		 * 设置状态 
		 * @param pos 位置
		 * @param state 状态
		 * 
		 */
		public function setState(pos:int,state:int):void
		{
			if(pos >= 9)
			{
				return;
			}
			_item[pos].able = Boolean(state);
		}
		
		/**
		 * 设置字 
		 * @param pos 位置
		 * @param str 字
		 * 
		 */
		public function setWord(pos:int,str:int):void
		{
			if(pos >= 9)
			{
				return;
			}
			_item[pos].word = str;
		}
		
		/**
		 * 设置仙玉 
		 * @param pos
		 * @param str
		 * 
		 */
		public function setMoney(pos:int,str:int,showNow:Boolean=false):void
		{
			if(pos >= 9)
			{
				return;
			}
			_item[pos].money		= str;
			_item[pos].showMoney	= showNow;
		}
		
		/**
		 * 添加点击事件 
		 * @param type
		 * @param listener
		 * 
		 */
		public function addItemEvent(type:String,listener:Function):void
		{
			for(var i:int=0;i<_item.length;i++)
			{
				_item[i].addItemEvent(type,listener,i);
			}
		}
		
		/**
		 * 检查状态 
		 * @param pos
		 * @return 
		 * 
		 */
		public function checkState(pos:int):Boolean
		{
			return _item[pos].able;
		}
		
		/**
		 * 播放动画 
		 * 
		 */
		public function playAnime(posArr:Array,onComplete:Function=null):void
		{
			clearAll();
			_highBg.visible = true;
			_posArr = posArr.sort(Array.NUMERIC);
			_func = onComplete;
			playAsc(0,_posArr[1]);
		} 
		
		/**
		 * 顺序动画 
		 * @param pos
		 * 
		 */
		private function playAsc(i:int,pos:int):void
		{
			//添加动画
			var timeLine	:TimelineLite =new TimelineLite({onComplete:onOpenWord,onCompleteParams:[pos]});
			timeLine.timeScale = 2;
			for(i;i<pos+1;i++)
			{
				timeLine.append(new TweenLite(_highBg, 0, {x:_item[i].x, y:_item[i].y, delay:PLAYSPEED}));
			}
		}
		
		/**
		 * 倒序动画 
		 * @param i
		 * @param pos
		 * 
		 */
		private function playDesc(i:int,pos:int):void
		{
			//添加动画
			var timeLine	:TimelineLite =new TimelineLite({onComplete:onOpenWord,onCompleteParams:[pos]});
			timeLine.timeScale = 2;
			for(i;i>pos-1;i--)
			{
				timeLine.append(new TweenLite(_highBg, 0, {x:_item[i].x, y:_item[i].y, delay:PLAYSPEED}));
			}
		}
		
		/**
		 * 点亮字 
		 * 
		 */
		private function onOpenWord(pos:int):void
		{
			_item[pos].able = true;
			if(pos == _posArr[1])
			{
				playDesc(pos,_posArr[0]);
			}
			else if(pos == _posArr[0])
			{
				playAsc(pos,_posArr[2]);
			}
			else
			{
				_highBg.visible = false;
				showAllMoney();
				if(null != _func)
				{
					_func();
				}
			}
		}
		
		/**
		 * 显示所有金钱 
		 * 
		 */
		public function showAllMoney():void
		{
			for(var i:int=0;i<_item.length;i++)
			{
				_item[i].showMoney	= true;
			}
		}
		
		/**
		 * 清除所有金钱 
		 * 
		 */
		public function clearMoney():void
		{
			for(var i:int=0;i<_item.length;i++)
			{
				_item[i].money	= 0;
			}
		}
		
		/**
		 * 清除全部状态 
		 * 
		 */
		public function clearAll():void
		{
			for(var i:int=0;i<_item.length;i++)
			{
				_item[i].able = false;
			}
		}
	}
}